package MyServlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Candidate;
import model.Comment;
import model.Interview;
import model.User;
import service.ServiceException;
import ServletService.Service;

/**
 * Servlet implementation class CrudInterview
 */
public class CrudInterview extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String option;
	private Service service;
	private Interview interview;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CrudInterview() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		option = request.getParameter("option");

		switch (option) {
		case "add": {
			interview = new Interview();
			Candidate candidate;
			String dateAsString = request.getParameter("date");
			DateFormat formatter;
			Date date = null;
			formatter = new SimpleDateFormat("MM/dd/yyyy");
			try {
				date = (Date) formatter.parse(dateAsString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String name = request.getParameter("firstName");
			candidate = Service.getCandidateService().getCandidateByName(name);
			String username = request.getParameter("userName");
			User user = Service.getUserService().getUserByUsername(username);
			// String[] selectedUsers = request
			// .getParameterValues("selectedUsers");
			// List<User> selUsers = new ArrayList<User>();
			// if (selectedUsers != null) {
			// for (int i = 0; i < selectedUsers.length; i++) {
			// selUsers.add(Service.getUserService().getUserByUsername(
			// selectedUsers[i]));
			// }
			// interview.setDate(date);
			// interview.setInterviewers(selUsers);
			// candidate = new Candidate();
			// candidate.setFirstName("testulet");
			// interview.setCandidate(candidate);
			List<User> users = new ArrayList<User>();
			users.add(user);
			interview.setDate(date);
			interview.setInterviewers(users);
			interview.setCandidate(candidate);
			Service.getInterviewService().insertInterview(interview);

			break;
		}

		case "update": {
			Service.getInterviewService().updateInterview(interview);

			break;
		}
		case "delete": {
			Service.getInterviewService().deleteInterview(interview);

			break;
		}
		case "getAll": {
			Service.getInterviewService().getAll();

			break;
		}
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/login");
		rd.forward(request, response);
	}

}
